package cn.itcast.nems.order.web.controller.b;

import cn.itcast.ic.common.bean.Page;
import cn.itcast.ic.common.exception.v2.BizExceptionProducer;
import cn.itcast.nems.base.dto.DiscountsDTO;
import cn.itcast.nems.common.enumeration.DiscountVisibilityEnum;
import cn.itcast.nems.common.enumeration.NemsErrorBodyEnum;
import cn.itcast.nems.common.util.NumberUtil;
import cn.itcast.nems.common.web.vo.BasePageRequest;
import cn.itcast.nems.discounts.dao.dataobject.EmsDiscountsDO;
import cn.itcast.nems.oldems.service.DiscountsService;
import cn.itcast.nems.order.web.vo.DiscountsPageQueryVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import jodd.util.StringUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 优惠管理
 * </p>
 *
 * @author zhouwei
 * @since 2023/8/29 1:56 PM
 **/
@RestController
@RequestMapping("/b/discounts")
@Slf4j
@RequiredArgsConstructor
public class DiscountsController {

    private final DiscountsService discountsService;

    /**
     * EMS优惠项分页查询
     *
     * @param queryParam 参数对象
     * @return ResponseEntity<IPage < EmsDiscounts>>
     **/
    @PostMapping("/page/list")
    public ResponseEntity<Page<DiscountsDTO>> pageList(@RequestBody BasePageRequest<DiscountsPageQueryVO> queryParam) {
        log.info("[优惠项分页查询] 请求参数: {} ", queryParam.toString());
        queryParam.convert();
        Page<DiscountsDTO> result = Page.of(queryParam.getPageNumber(), queryParam.getPageSize(), 0, null);
        if (Objects.isNull(queryParam.getCondition())) {
            queryParam.setCondition(new DiscountsPageQueryVO());
        }
        if (!NumberUtil.checkNum(queryParam.getCondition().getId(), null)) {
            log.info("[优惠项分页查询] 请求参数 编号: {}, 包含非数字内容", queryParam.getCondition().getId());
            return ResponseEntity.ok().body(result);
        }
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<EmsDiscountsDO> page
                = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>();
        page.setCurrent(queryParam.getPageNumber());
        page.setSize(queryParam.getPageSize());
        List<Integer> visibilityList = null;
        if (StringUtil.isNotBlank(queryParam.getCondition().getVisibilityType())) {
            try {
                visibilityList = DiscountVisibilityEnum.valueOf(queryParam.getCondition().getVisibilityType()).getVisibilityList();
            } catch (Exception ignored) {
                BizExceptionProducer.throwProduce(NemsErrorBodyEnum.ENUM_ERROR);
            }
        }
        IPage<EmsDiscountsDO> data = discountsService.findPageList(page,
                new EmsDiscountsDO(
                        NumberUtil.format(queryParam.getCondition().getId(), 11),
                        queryParam.getCondition().getDiscountsName(),
                        visibilityList
                )
        );
        result = Page.of(
                Long.valueOf(page.getCurrent()).intValue(),
                Long.valueOf(page.getSize()).intValue(),
                Long.valueOf(data.getTotal()).intValue(),
                null);
        if (!CollectionUtils.isEmpty(data.getRecords())) {
            result.setRecords(data.getRecords().stream().map(item -> {
                DiscountsDTO discountsDTO = new DiscountsDTO();
                BeanUtils.copyProperties(item, discountsDTO);
                discountsDTO.setDiscountsMoney(NumberUtil.format(item.getDiscountsMoney(), 2, ","));
                discountsDTO.setDiscountsRate(NumberUtil.format(item.getDiscountsRate(), 2, ","));
                return discountsDTO;
            }).toList());
        }
        return ResponseEntity.ok().body(result);
    }
}
